<template>
  <div>
    <el-card class="card">
      <!-- 搜索区域 -->
      <div class="search-bar">
        <el-input
            v-model="searchName"
            placeholder="请输入节点名称"
            clearable
            style="width: 300px;"
            @keyup.enter="fetchNodeData"
        />
        <el-button type="primary" @click="fetchNodeData" :loading="loading">搜索</el-button>
      </div>

      <!-- 搜索结果展示 -->
      <div v-if="nodeData.node" class="result-container">
        <!-- 目标节点信息 -->
        <el-descriptions title="目标节点信息" :column="2" border class="node-info">
          <el-descriptions-item label="ID">{{ nodeData.node.id }}</el-descriptions-item>
          <el-descriptions-item label="名称">{{ nodeData.node.name }}</el-descriptions-item>
          <el-descriptions-item label="标签">{{ nodeData.node.label }}</el-descriptions-item>
          <el-descriptions-item label="类型">{{ nodeData.node.type }}</el-descriptions-item>
          <el-descriptions-item label="描述" :span="2">{{ nodeData.node.description || '无' }}</el-descriptions-item>
        </el-descriptions>

        <!-- 关系和相连节点表格 -->
        <el-table
            :data="nodeData.links"
            style="width: 100%; margin-top: 20px;"
            border
            stripe
            empty-text="暂无关系数据"
        >
          <el-table-column prop="type" label="关系类型" width="150" />
          <el-table-column label="相连节点" width="200">
            <template #default="{ row }">
              {{ getRelatedNodeName(row.target) }}
            </template>
          </el-table-column>
          <el-table-column prop="description" label="关系描述" />
        </el-table>
      </div>

      <!-- 无数据提示 -->
      <el-empty v-else description="请搜索一个节点以查看其关系" />
    </el-card>
  </div>

</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import axios from "axios";

// 搜索输入框绑定值
const searchName = ref('');
// 节点数据
const nodeData = ref({
  node: null,
  links: [],
  relatedNodes: []
});
// 加载状态
const loading = ref(false);

// 获取节点数据
const fetchNodeData = async () => {
  if (!searchName.value.trim()) {
    ElMessage.warning('请输入节点名称');
    return;
  }
  loading.value = true;
  let name = searchName.value
  try {
    const { data } = await axios.get(`/api/graph/node/${name}`)
    console.log('查询数据',data)
    if (data.code === 200) {
      nodeData.value = data.data;
      ElMessage.success('查询成功');
    } else {
      nodeData.value = { node: null, links: [], relatedNodes: [] };
      ElMessage.error(data.message);
    }
  } catch (error) {
    nodeData.value = { node: null, links: [], relatedNodes: [] };
    ElMessage.error('查询失败');
    console.error('Error:', error);
  } finally {
    loading.value = false;
  }
};

// 根据 target ID 获取相连节点名称
const getRelatedNodeName = (targetId) => {
  const relatedNode = nodeData.value.relatedNodes.find(node => node.id === targetId);
  return relatedNode ? relatedNode.name : '未知节点';
};
</script>

<style scoped>
.card {
  padding-top: 2%;
  width: 100vh;
  height: 100vh;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.search-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.result-container {
  width: 100vh;
  margin-top: 20px;
}

.node-info {
  margin-bottom: 20px;
}
</style>